﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SusyGame_Model
{
    public class Enemy : Sprite
    {
        //Atributos
        protected float startHealth;
        protected float currentHealth;
        protected bool alive = true;
        protected float speed = 0.5f;
        protected int bountyGiven;

        protected Pool pool;
        protected Ground ground;

        private Queue<Vector2> waypoints = new Queue<Vector2>();

        public float CurrentHealth
        {
            get { return currentHealth; }
            set { currentHealth = value; }
        }

        protected int BountyGiven
        {
            get { return bountyGiven; }
            set { bountyGiven = value; }
        }

        public bool IsDead
        {
            get { return CurrentHealth <= 0; }
        }

        public Enemy(Texture2D texture, Vector2 position, float health, int bountyGiven, float speed)
            : base(texture, position)
        {
            this.startHealth = health;
            this.currentHealth = startHealth;
            this.bountyGiven = bountyGiven;
            this.speed = speed;
        }

        public void SetWaypoints(Queue<Vector2> waypoints)
        {
            foreach (Vector2 waypoint in waypoints)
                this.waypoints.Enqueue(waypoint);

            base.Position = this.waypoints.Dequeue();
        }

        public float DistanceToDestination
        {
            get { return Vector2.Distance(base.Position, waypoints.Peek()); }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (waypoints.Count > 0)
            {
                if (DistanceToDestination < speed)
                {
                    base.Position = waypoints.Peek();
                    waypoints.Dequeue();
                }

                else
                {
                    Vector2 direction = waypoints.Peek() - base.Position;
                    direction.Normalize();

                    base.Velocity = Vector2.Multiply(direction, speed);

                    base.Position += base.Velocity;
                }
            }

            else
                alive = false;

            if (CurrentHealth <= 0)
                alive = false;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (alive)
            {
                float healthPercentage = (float)CurrentHealth / (float)startHealth;

                Color color = new Color(new Vector3(1 - healthPercentage, 1 - healthPercentage, 1 - healthPercentage));

                base.Draw(spriteBatch, color);
            }
        }
        //private String name;
        //private String description;
        //private int healthPoints;
        //private int walkSpeed;
        //private int typeLand; //Tipo de suelo en el que va a ser plantado
        //private int type; //Tipo de enemigo
        //private char icon;

        //Propiedades
        //public String Name
        //{
        //    get { return name; }
        //    set { name = value; }
        //}        

        //public String Description
        //{
        //    get { return description; }
        //    set { description = value; }
        //}        

        //public int HealthPoints
        //{
        //    get { return healthPoints; }
        //    set { healthPoints = value; }
        //}        

        //public int WalkSpeed
        //{
        //    get { return walkSpeed; }
        //    set { walkSpeed = value; }
        //}
       
        //public int TypeLand
        //{
        //    get { return typeLand; }
        //    set { typeLand = value; }
        //}
        
        //public int Type
        //{
        //    get { return type; }
        //    set { type = value; }
        //}        

        //public char Icon
        //{
        //    get { return icon; }
        //    set { icon = value; }
        //}        

        
        //Constructores
        //public Enemy()
        //{
        //}

        //public Enemy(int type)
        //{
        //    if (type == 1)
        //    {
        //        //Le asignamos los valores de los atributos de los enemigos
        //        this.Name = "Marco Tulio";
        //        this.Description = "Descripcion enemigo tipo Marco Tulio";
        //        this.HealthPoints = 100;
        //        this.WalkSpeed = 1;
        //        this.TypeLand = 1;
        //        this.Icon = 'M';
        //    }
        //    else
        //    {
        //        if (type == 2)
        //        {
        //            this.Name = "Castañeda";
        //            this.Description = "Descripcion enemigo tipo Castañeda";
        //            this.HealthPoints = 150;
        //            this.WalkSpeed = 2;
        //            this.TypeLand = 2;
        //            this.Icon = 'C';
        //        }
        //    }
        //    this.type = type;
        //}
    }
}
